﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using ProgNetComponents.Controls;
using System.Drawing;
using ProgNetComponents.Interfaces;
using ProgNetComponents.Classes;
using ProgNetComponents.Collections;
using ProgNetComponentsDx.Design.Designers;

namespace ProgNetComponentsDx.Options.Design
{
    public class OptionGroupCollectionEditorForm : BaseGroupCollectionDesigner
    {

        private TextBox txtEditor;
        private Label lblValue;
        private ComboBox comboType;
        private Label lblType;
        private ComboBox cmbEditor;
        private DevExpress.XtraEditors.MemoExEdit memoExEdit1;
        private DateTimePicker dtEditor;
    
        public OptionGroupCollectionEditorForm()
        {
            InitializeComponent();
            SetTypes();
            this.comboType.DataSource = coll;
            this.comboType.SelectedIndex = 0;
        }
        
        private void InitializeComponent(){
            this.txtEditor = new System.Windows.Forms.TextBox();
            this.lblValue = new System.Windows.Forms.Label();
            this.comboType = new System.Windows.Forms.ComboBox();
            this.lblType = new System.Windows.Forms.Label();
            this.cmbEditor = new System.Windows.Forms.ComboBox();
            this.dtEditor = new System.Windows.Forms.DateTimePicker();
            this.memoExEdit1 = new DevExpress.XtraEditors.MemoExEdit();
            this.panel2.SuspendLayout();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.memoExEdit1.Properties)).BeginInit();
            this.SuspendLayout();
            // 
            // panel2
            // 
            this.panel2.Size = new System.Drawing.Size(52, 262);
            // 
            // treeView1
            // 
            this.treeView1.LineColor = System.Drawing.Color.Black;
            this.treeView1.Size = new System.Drawing.Size(204, 188);
            // 
            // btnAddRoot
            // 
            this.btnAddRoot.Location = new System.Drawing.Point(3, 210);
            // 
            // label1
            // 
            this.label1.Size = new System.Drawing.Size(204, 20);
            // 
            // propertyGrid1
            // 
            this.propertyGrid1.Size = new System.Drawing.Size(224, 262);
            // 
            // btnAddChild
            // 
            this.btnAddChild.Location = new System.Drawing.Point(108, 210);
            // 
            // btnAddOption
            // 
            this.btnAddOption.Location = new System.Drawing.Point(54, 236);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.txtEditor);
            this.panel1.Controls.Add(this.cmbEditor);
            this.panel1.Controls.Add(this.memoExEdit1);
            this.panel1.Controls.Add(this.lblType);
            this.panel1.Controls.Add(this.lblValue);
            this.panel1.Controls.Add(this.dtEditor);
            this.panel1.Controls.Add(this.comboType);
            this.panel1.Location = new System.Drawing.Point(0, 262);
            this.panel1.Size = new System.Drawing.Size(484, 52);
            this.panel1.Controls.SetChildIndex(this.comboType, 0);
            this.panel1.Controls.SetChildIndex(this.dtEditor, 0);
            this.panel1.Controls.SetChildIndex(this.lblValue, 0);
            this.panel1.Controls.SetChildIndex(this.lblType, 0);
            this.panel1.Controls.SetChildIndex(this.memoExEdit1, 0);
            this.panel1.Controls.SetChildIndex(this.cmbEditor, 0);
            this.panel1.Controls.SetChildIndex(this.txtEditor, 0);
            this.panel1.Controls.SetChildIndex(this.btnOk, 0);
            this.panel1.Controls.SetChildIndex(this.btnCancel, 0);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(397, 17);
            // 
            // btnOk
            // 
            this.btnOk.Location = new System.Drawing.Point(316, 17);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Size = new System.Drawing.Size(484, 262);
            this.splitContainer1.SplitterDistance = 204;
            // 
            // txtEditor
            // 
            this.txtEditor.Location = new System.Drawing.Point(92, 29);
            this.txtEditor.Name = "txtEditor";
            this.txtEditor.Size = new System.Drawing.Size(150, 20);
            this.txtEditor.TabIndex = 8;
            this.txtEditor.Visible = false;
            // 
            // lblValue
            // 
            this.lblValue.Location = new System.Drawing.Point(3, 29);
            this.lblValue.Name = "lblValue";
            this.lblValue.Size = new System.Drawing.Size(83, 20);
            this.lblValue.TabIndex = 11;
            this.lblValue.Text = "Default Value:";
            this.lblValue.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // comboType
            // 
            this.comboType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboType.Location = new System.Drawing.Point(92, 6);
            this.comboType.Name = "comboType";
            this.comboType.Size = new System.Drawing.Size(150, 21);
            this.comboType.TabIndex = 9;
            this.comboType.SelectedIndexChanged += new System.EventHandler(this.comboType_SelectedIndexChanged);
            // 
            // lblType
            // 
            this.lblType.Location = new System.Drawing.Point(3, 7);
            this.lblType.Name = "lblType";
            this.lblType.Size = new System.Drawing.Size(83, 20);
            this.lblType.TabIndex = 10;
            this.lblType.Text = "Default Type:";
            this.lblType.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // cmbEditor
            // 
            this.cmbEditor.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbEditor.Items.AddRange(new object[] {
            "True",
            "False"});
            this.cmbEditor.Location = new System.Drawing.Point(92, 29);
            this.cmbEditor.Name = "cmbEditor";
            this.cmbEditor.Size = new System.Drawing.Size(150, 21);
            this.cmbEditor.TabIndex = 7;
            this.cmbEditor.Visible = false;
            // 
            // dtEditor
            // 
            this.dtEditor.Format = System.Windows.Forms.DateTimePickerFormat.Short;
            this.dtEditor.Location = new System.Drawing.Point(92, 29);
            this.dtEditor.Name = "dtEditor";
            this.dtEditor.Size = new System.Drawing.Size(150, 20);
            this.dtEditor.TabIndex = 6;
            this.dtEditor.Visible = false;
            // 
            // memoExEdit1
            // 
            this.memoExEdit1.Location = new System.Drawing.Point(92, 29);
            this.memoExEdit1.Name = "memoExEdit1";
            this.memoExEdit1.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.memoExEdit1.Properties.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.memoExEdit1.Properties.ShowIcon = false;
            this.memoExEdit1.Properties.WordWrap = false;
            this.memoExEdit1.Size = new System.Drawing.Size(150, 20);
            this.memoExEdit1.TabIndex = 12;
            this.memoExEdit1.Visible = false;
            // 
            // OptionGroupCollectionEditorForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.ClientSize = new System.Drawing.Size(484, 314);
            this.MinimumSize = new System.Drawing.Size(500, 350);
            this.Name = "OptionGroupCollectionEditorForm";
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.panel2.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.memoExEdit1.Properties)).EndInit();
            this.ResumeLayout(false);

        }

        protected override INamedGroup CreateNewGroup()
        {
            return new OptionGroup("NowaGrupa");
        }

        protected override INamedObject CreateNewItem()
        {
            Option o = new Option();
            o.Name = "NowaOpcja";
            if (comboType.SelectedIndex > 0)
                o.OptionType = coll[comboType.SelectedIndex].Value as Type;
            o.DefaultValue = EditValue;
            return o;
        }

        #region Default Option Type/value
        private object objEditValue;
        private object EditValue
        {
            get
            {
                Control editor = Editor;
                if (editor != null)
                {
                    NamedObject o = comboType.SelectedItem as NamedObject;
                    if (o != null && o.Value != null)
                    {
                        if (editor == memoExEdit1)
                        {
                            return editor.Text;
                        }
                        else if (editor == txtEditor)
                        {
                            string str = txtEditor.Text;
                            if (str.IsNull())
                                return null;
                            if ((Type)(o.Value) == typeof(decimal))
                                return str.AsDecimal();
                            if ((Type)(o.Value) == typeof(int))
                                return str.AsInt();
                            return str;
                        }
                        else if (editor == cmbEditor)
                        {
                            if (cmbEditor.SelectedIndex == 0)
                                return true;
                            return false;
                        }
                        else
                        {
                            if (dtEditor.Text.IsNotNull())
                                return dtEditor.Value;
                        }
                    }
                }
                return null;
            }
        }
        private Control oldEditor;
        private Control Editor
        {
            get
            {
                switch (this.comboType.SelectedIndex)
                {
                    case 0:
                        return null;
                    case 5:
                        return this.cmbEditor;
                    case 6:
                        return this.dtEditor;
                    case 4:
                        return this.memoExEdit1;
                    default:
                        return this.txtEditor;
                }
            }
        }
        NamedObjectCollection coll = new NamedObjectCollection();
        private void SetTypes()
        {
            coll.Add(new NamedObject("<NULL>", null));
            AddType(typeof(int));
            AddType(typeof(decimal));
            AddType(typeof(string));
            coll.Add(new NamedObject("String (Multiline)", typeof(string)));
            AddType(typeof(bool));
            AddType(typeof(DateTime));
        }

        private void AddType(Type t)
        {
            coll.Add(new NamedObject(t.Name, t));
        }

        private void comboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Control editor = this.Editor;
            if (editor != null)
            {
                if (this.oldEditor != null && this.oldEditor == this.txtEditor)
                    this.objEditValue = this.oldEditor.Text;

                this.txtEditor.Visible = editor == txtEditor;
                this.cmbEditor.Visible = editor == cmbEditor;
                this.dtEditor.Visible = editor == dtEditor;
                this.memoExEdit1.Visible = editor == memoExEdit1;

                if (editor == cmbEditor)
                {
                    this.cmbEditor.SelectedIndex = this.objEditValue.AsBool() ? 0 : 1;
                }
                else if (editor.Equals((object)this.dtEditor))
                {
                    try
                    {
                        this.dtEditor.Value = this.objEditValue.AsDateTime(DateTime.Now);
                    }
                    catch
                    {
                        this.dtEditor.Value = DateTime.Now;
                    }
                }
                else
                    editor.Text = this.objEditValue.AsString();
                this.oldEditor = editor;
            }
            else
                this.txtEditor.Visible = this.cmbEditor.Visible = this.dtEditor.Visible = this.memoExEdit1.Visible = false;
        }

        #endregion

    }
}
